/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.resource.endpoint;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.github.classgraph.json.JSONUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.common.constant.TrainingRoleConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.enums.AttachType;
import org.springblade.modules.resource.service.IAttachService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 对象存储端点
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@Api(value = "考核系统对象存储", tags = "对象存储端点")
@RequestMapping( "training/oss/endpoint")
public class TrainingOssEndpoint {

	/**
	 * 对象存储构建类
	 */
	private final OssBuilder ossBuilder;

	/**
	 * 附件表服务
	 */
	private final IAttachService attachService;


	/**
	 * 上传课程视频文件并保存至附件表
	 * @param fileName 存储桶对象名称
	 * @param file     文件
	 * @return ObjectStat
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.UPLOAD,describe = "上传课程视频")
	@SneakyThrows
	@ApiOperationSupport(order = 1)
	@PostMapping("/course/vedio")
	@ApiOperation(value = "上传课程视频文件", notes = "传入文件,文件名按需传入")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R<BladeFile> putFileAttachVedio(@RequestParam(required = false) String fileName, @RequestParam MultipartFile file) {
		if (StringUtils.isBlank(fileName)) {
			fileName=file.getOriginalFilename();
		}else {
			fileName =fileName+"."+FileUtil.getFileExtension(file.getOriginalFilename());
		}
		BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
		Long attachId = buildAttach(fileName, file.getSize(), bladeFile,AttachType.COURSE_VEDIO.getCategory());
		bladeFile.setAttachId(attachId);
		return R.data(bladeFile);
	}

	/**
	 * 上传课程学习课件文件并保存至附件表
	 * @param fileName 存储桶对象名称
	 * @param file     文件
	 * @return ObjectStat
	 */
	@ServiceLog(value = TrainingOperateConstant.COURSE,operate = TrainingOperateConstant.UPLOAD,describe = "上传学习课件")
	@SneakyThrows
	@ApiOperationSupport(order = 2)
	@PostMapping("/course/courseware")
	@ApiOperation(value = "上传课程学习课件", notes = "传入文件,文件名按需传入")
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	public R<BladeFile> putFileAttach(@RequestParam(required = false) String fileName, @RequestParam MultipartFile file) {
		if (StringUtils.isBlank(fileName)) {
			fileName=file.getOriginalFilename();
		}else {
			fileName =fileName+"."+FileUtil.getFileExtension(file.getOriginalFilename());
		}
		BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
		Long attachId = buildAttach(fileName, file.getSize(), bladeFile,AttachType.COURSE_COURSEWARE.getCategory());
		bladeFile.setAttachId(attachId);
		return R.data(bladeFile);
	}



	/**
	 * 构建附件表
	 *
	 * @param fileName  文件名
	 * @param fileSize  文件大小
	 * @param bladeFile 对象存储文件
	 * @param category  文件所属业务类型
	 *
	 * @return attachId
	 */
	private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile,Integer category) {
		String fileExtension = FileUtil.getFileExtension(fileName);
		Attach attach = new Attach();
		attach.setDomainUrl(bladeFile.getDomain());
		attach.setLink(bladeFile.getLink());
		attach.setName(bladeFile.getName());
		attach.setOriginalName(bladeFile.getOriginalName());
		attach.setAttachSize(fileSize);
		attach.setExtension(fileExtension);
		attach.setAttachType(category);
		attachService.save(attach);
		return attach.getId();
	}


}
